package com.tianqu.tms.www.finance.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.TransferFinanceDetailDto;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferFinance;
import com.tianqu.tms.www.finance.actionform.CarProxyPaySearchInputForm;
import com.tianqu.tms.www.finance.controller.CarProxyPayTransferResultController;
import com.tianqu.tms.www.finance.viewhelper.CarProxyPaySearchInputViewHelper;

public class CarProxyPayTransferResultAction extends TmsAction{

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		CarProxyPaySearchInputForm inputForm = (CarProxyPaySearchInputForm)form;
		//check
		if(StringUtil.isNullOrBlank(StringUtil.removeDH(inputForm.getPrice()))){
			String[] param = {"收费金额"};
			setErrorsToRequest(request, TmsConstants.U0005, param);
			return mapping.getInputForward();
		}
		
        if(!StringUtil.isInteger(StringUtil.removeDH(inputForm.getPrice()))){
        	String[] param = {"收费金额"};
			setErrorsToRequest(request, TmsConstants.U0015, param); 
			return mapping.getInputForward();	
        }
		
        if("1".equals(inputForm.getSaveType())){
        	if("0".equals(inputForm.getPrice()) && "".equals(inputForm.getRemarks())){
    			String[] param = {"收费金额"};
    			setErrorsToRequest(request, TmsConstants.U1603, param);
    			return mapping.getInputForward();
    		}
        }else if("2".equals(inputForm.getSaveType())){
        	TransferFinance transferFinance = new TransferFinance();
        	List<TransferFinanceDetailDto> list = transferFinance.getTransferFinanceList(inputForm.getSerial(), "1",inputForm.getTransferPriceChargeType()+"");//收费
            
        	if(list.size() == 0){
    			setErrorsToRequest(request, TmsConstants.U1604, null);
    			return mapping.getInputForward();
        	}
            if(list.size() > 0){
            	TransferFinanceDetailDto detailDto = list.get(0);
            	if(detailDto.getPrice() < Integer.parseInt(StringUtil.removeDH(inputForm.getPrice()))){
            		String[] param = {detailDto.getPrice()+""};
        			setErrorsToRequest(request, TmsConstants.U1605, param);
        			return mapping.getInputForward();
            	}
            }
            //
            TransferAcceptedInfo accept = new TransferAcceptedInfo();
    		accept = accept.findTransStatusBySerial(inputForm.getSerial());
    		if(accept!=null && accept.getStatus() == 16){
    			setErrorsToRequest(request, TmsConstants.U1606, null);
    			return mapping.getInputForward();
    		}
    		//
    		if(inputForm.getTransferPriceChargeType() == 9) {
    			accept = accept.getAssessInfoBySerial(inputForm.getSerial());
        		if(accept!=null) {
        			if(accept.getStatus() == 0 && accept.getChangeUsenature() == 2) {
        				setErrorsToRequest(request, TmsConstants.U1610, null);
            			return mapping.getInputForward();
        			}else if(accept.getStatus() != 0 ) {
        				setErrorsToRequest(request, TmsConstants.U1609, null);
            			return mapping.getInputForward();
        			}
        		}
    		}
        }
		
		 
		CarProxyPayTransferResultController controller = new CarProxyPayTransferResultController();
		controller.execute(uc);
		
		//
		TransferAcceptedInfo accept = new TransferAcceptedInfo();
		accept = accept.getAcceptInfoBySerial(inputForm.getSerial(), null, null);
		CarProxyPaySearchInputViewHelper viewhelper = new CarProxyPaySearchInputViewHelper();
		if(accept != null && "1".equals(inputForm.getSaveType())) {
			viewhelper.setPrintSerial(inputForm.getSerial());
			viewhelper.setPrintLicensePlate(accept.getLicensePlate());
			viewhelper.setPrintPrice(inputForm.getPrice());
			viewhelper.setPrint("1");
			viewhelper.setReason(getReason(inputForm.getTransferPriceChargeType()));
		}else {
			viewhelper.setPrint("0");
		}
		uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);
		request.setAttribute(TmsConstants.VIEWHELP, viewhelper);

		//
		return mapping.findForward(TmsConstants.SUCCESS);
	}

	private String getReason(int v) {
		String r = "";
		if(v == 1) {
			r = "变更名头";
		}else if(v == 2) {
			r = "变更三证合一";
		}else if(v == 3) {
			r = "换买主";
		}else if(v == 4) {
			r = "换地址";
		}else if(v == 5) {
			r = "换价格";
		}else if(v == 6) {
			r = "补牌";
		}else if(v == 7) {
			r = "补证";
		}else if(v == 8) {
			r = "补书";
		}else if(v == 9) {
			r = "交易服务费";
		}else if(v == 10) {
			r = "待办服务费";
		}else if(v == 11) {
			r = "待办服务续费";
		}else if(v == 12) {
			r = "变更交易票";
		}else if(v == 13) {
			r = "照片处理";
		}else if(v == 14) {
			r = "过期解锁续办";
		}
		return r;
	}
}
